package com.kingsalert.kingsalert.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.config.CountryConfig;
import com.kingsalert.kingsalert.util.http.HttpUtil;


public class BindAccountDialog_CoteWave extends Dialog {

    private final FragmentActivity activity;

    private static final String Channel_Wave ="WAVE", Channel_Others ="OTHERS";

    private String accountChannel = Channel_Wave;

    private final String page;

    private EditText mobile1Et;
    private EditText mobile2Et;

    private final Runnable addActRespRunnable;

    public BindAccountDialog_CoteWave(@NonNull FragmentActivity activity,String page,Runnable addActRespRunnable) {
        super(activity);
        this.page = page;
        this.addActRespRunnable = addActRespRunnable;
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bindaccount_cotewave);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        findViewById(R.id.dbc_ib_back).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_CLOSE_CLICK","");
            dismiss();
        });

        findViewById(R.id.dbc_ib_customerservice).setOnClickListener(v -> {
            new CustomerServiceDialog(getContext()).show();
        });

        //绑卡提交
        findViewById(R.id.dbc_btn_add).setOnClickListener(v -> {

            HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_SAVE_CLICK","");

            String mobile1 = mobile1Et.getText().toString().trim();
            String mobile2 = mobile2Et.getText().toString().trim();

            if (TextUtils.isEmpty(mobile1)) mobile1 = "";
            if (TextUtils.isEmpty(mobile2)) mobile2 = "";

            HttpUtil.getInstance().addAccount_Fr(activity,mobile1,mobile2,accountChannel,() -> {
                if (addActRespRunnable!=null) addActRespRunnable.run();
            });

            dismiss();

        });

        ImageView accountChannelIv = findViewById(R.id.dbc_iv_accountchaccel);
        TextView accountChannelTv = findViewById(R.id.dbc_tv_accountcannel);
        TextView areacode1Tv = findViewById(R.id.dbc_tv_areacode1);
        TextView areacode2Tv = findViewById(R.id.dbc_tv_areacode2);
        mobile1Et = findViewById(R.id.dbc_et_mobile1);
        mobile2Et = findViewById(R.id.dbc_et_mobile2);
        Button changeChannelBtn = findViewById(R.id.dbc_btn_changechannel);

        areacode1Tv.setText(CountryConfig.getInstance().getAreaCode());
        areacode2Tv.setText(CountryConfig.getInstance().getAreaCode());

        changeChannelBtn.setOnClickListener(v -> {
            if (accountChannel.equalsIgnoreCase(Channel_Wave)){//切换到Other
                HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_TO_OTHER_CLICK","");
                accountChannel = Channel_Others;
                accountChannelIv.setBackgroundResource(R.drawable.img_account_type_fr_others);
                accountChannelTv.setText("Orange/MTN/MOOV");
                changeChannelBtn.setText("Je veux utiliser Wave");
            }else{//切换到Wave
                HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_TO_WAVE_CLICK","");
                accountChannel = Channel_Wave;
                accountChannelIv.setBackgroundResource(R.drawable.img_account_type_fr_wave);
                accountChannelTv.setText("Wave");
                changeChannelBtn.setText("Je n'ai pas Wave");
            }
        });

        mobile1Et.setOnFocusChangeListener((v, hasFocus) ->
            HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_PHONE_INPUT",hasFocus ? "":mobile1Et.getText().toString().trim())
        );

        mobile2Et.setOnFocusChangeListener((v, hasFocus) ->
            HttpUtil.getInstance().saveBuriedPoint(page,accountChannel+"_PHONE_CONFIRM_INPUT",hasFocus ? "":mobile2Et.getText().toString().trim())
        );
    }

}
